package com.hllxd.hism.star.basic.service.controller2;

import com.hllxd.hism.framework.httprpc.service.*;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.TraceImc;
import com.hllxd.hism.star.basic.service.model.TraceImcTags;
import com.hllxd.hism.star.basic.service.model.UserTrace;
import com.hllxd.hism.star.basic.service.service.UserTraceService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Author yaofangjun
 * @Description https://docs.58corp.com/#/space/1619177925616140290
 * @Date 2023/2/14 11:35
 */
@Slf4j
@RestGrpcService
@RestGrpcMapping("/v1/user_trace")
public class UserTraceController2 {

    @Autowired
    private UserTraceService userTraceService;

    @Data
    public static class Param {
        public String ts1;// 开始时间精准到秒
        public String ts2;// 结束时间精准到秒
        public String search;
        public String userId;
        public String infoId;
        public Boolean hasExp;
    }

    @RestGrpcMapping("/findUserTrace.do")
    public NTSTATUS findUserTrace(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            List<UserTrace> items = userTraceService.findUserTrace(aa.ts1, aa.ts2, aa.userId, aa.hasExp);
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

//    @RestGrpcMapping("/queryTraceUserTags.do")
//    public NTSTATUS queryTraceUserTags(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
//        log.info("request params: {}", params);
//        try {
//            Param aa = params.driverObject(Param.class);
//            TraceUserTags items = userTraceService.queryTraceUserTags(aa.userId);
//            result.setData(items);
//            return NTSTATUS.SUCCESS_STATUS();
//        } catch (BusinessException e) {
//            log.warn("Exception: {}. {}", e, params);
//            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
//        } catch (Exception e) {
//            log.error("Exception: {}. {}", e, params, e);
//            return NTSTATUS.ERROR_STATUS("500", e.toString());
//        }
//    }

    @RestGrpcMapping("/queryTraceInfoTags.do")
    public NTSTATUS queryTraceImcTags(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            TraceImcTags items = userTraceService.queryTraceImcTags(aa.infoId);
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

    @RestGrpcMapping("/findTraceImc.do")
    public NTSTATUS findTraceImc(PDRIVER_OBJECT params, PRESULT_OBJECT result) {
        log.info("request params: {}", params);
        try {
            Param aa = params.driverObject(Param.class);
            List<TraceImc> items = userTraceService.findTraceImc(aa.ts1, aa.ts2, aa.infoId, aa.hasExp);
            result.setData(items);
            return NTSTATUS.SUCCESS_STATUS();
        } catch (BusinessException e) {
            log.warn("Exception: {}. {}", e, params);
            return NTSTATUS.ERROR_STATUS(e.code, e.msg);
        } catch (Exception e) {
            log.error("Exception: {}. {}", e, params, e);
            return NTSTATUS.ERROR_STATUS("500", e.toString());
        }
    }

}
